// use crate::sec::Encryptor;
// use crate::sec::Cryptor;
use std::process::Command;
// use serde_json::from_str;
// use serde_json::from_value;
// use serde_json::Value;
// use std::io::BufReader;
use std::fs::File;
use std::fs::create_dir;
use std::fs::read_dir;
use std::io::Read;
// use std::io::Write;
use std::error::Error;
use dirs::home_dir;
use colored::Colorize;
// lazy_static!{
//     static ref HashMap<String,String> = {
//         load()
//     }
// }
type O<T> = Result<T, Box<dyn Error>>;

pub fn load(name:&str) -> O<String>{
    let p = home_dir().unwrap().join(".config").join("scripts");
    let mut out = String::new();
    if p.exists(){
        log::info!("load {}", name.green());
        if name == "pull" || name =="push" {
            let cmd = if name == "push"{
                "git add -A ; git commit -m \"save\"; git push origin master".to_string()
            }else{
                "git pull origin master".to_string()
            };
            
            log::info!("git sync : cd  {} ;{}",p.to_str().unwrap(),cmd.yellow());
            let output = if cfg!(target_os = "windows") {
                Command::new("cmd")
                    .arg("/C")
                    .arg(format!("cd {} ;  {} ", p.to_str().unwrap(), &cmd))
                    .output()
                    .expect("failed to execute process")
            } else {
                Command::new("sh")
                    .arg("-c")
                    .arg(format!("cd {} ;{}", p.to_str().unwrap(), &cmd))
                    .output()
                    .expect("failed to execute process")
            };
            if !output.status.success() {
                out.push_str(
                    match std::str::from_utf8(&output.stderr) {
                        Ok(e) => e.to_string(),
                            _ => {"".to_string()}
                    }.as_str()
                );
            }else{
                out.push_str(
                    match std::str::from_utf8(&output.stdout) {
                        Ok(e) => e.to_string(),
                            _ => {"".to_string()}
                    }.as_str()
                );
            }
            log::info!("out {}", out.yellow());
            
            return Ok(out);
        }
        for entry in read_dir(p)?{
            let e = entry?.path();
            if e.is_dir(){continue}
            if e.to_str().unwrap().ends_with(".sh"){
                    
                if name == "ls"{
                    log::info!("{}", e.to_str().unwrap().green());
                    // out.push_str(&format!("\n{}",e.to_str().unwrap()))
                }else if e.to_str().unwrap().contains(name){
                    let mut f = File::open(e)?;
                    f.read_to_string(&mut out)?;
                    return Ok(out);
                }
            }
        }
    }else{
        create_dir(p)?;
        log::info!("need set git repository for scripts !");
    }
    Ok(out)
}

